package com.cuckoo.mall.thus.order.orderlist.paidorder;


import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.BaseListFragment;
import com.bgn.baseframe.utils.ToastUtil;
import com.bgn.baseframe.utils.decoration.BaseItemBottomDecoration;
import com.bgn.baseframe.utils.decoration.CommonDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.Order;
import com.cuckoo.mall.thus.utils.DialogHelper;

import java.util.List;

/**
 * 作者：wl on 2018/4/ * 邮箱：wanglun@stosz.com
 */

public class PaidOrderFragment extends BaseListFragment<PaidyorderContract.View, PaidorderPresenter, Order> implements PaidyorderContract.View {

    @Override
    protected boolean shoudOpenLoadMore() {
        return true;
    }

    @Override
    protected boolean shoudSetEmptyView() {
        return false;
    }

    @Override
    protected boolean shoudOpenRefresh() {
        return true;
    }

    @Override
    protected BaseQuickAdapter setAdapter() {
        adapter = new PaidOrderQiuckAdapter(datas);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                // ARouter.getInstance().build("/mall/orderdetailActivity").withString("id", datas.get(position).getOrder_number()).navigation();
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                switch (view.getId()) {
                    case R.id.tv_del:

                        DialogHelper.showConfirmDialog(getActivity(), getString(R.string.sure_delete_order), getString(R.string.delete), getString(R.string.cancel), new DialogHelper.DialogPressCallBack() {
                            @Override
                            public void whenPressRightButton() {
                                if (isNetworkerConnectHint()) {
                                    showLoadingView();
                                    mPresenter.deleteOrder(position, datas.get(position).getOrder_number());
                                }
                            }
                        });
                }
            }
        });
        return adapter;
    }

    @Override
    protected void setLayoutManage() {
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.addItemDecoration(new CommonDecoration(15));
    }

    @Override
    protected void initListData() {
        if (isNetworkerConnectHint()) {
            showLoadingView();
            mPresenter.loadOrderList(HTTP_INIT_LIST, "1", page_index, page_count);
        } else {
            showDefaultView();
        }
    }

    @Override
    protected void onRefreshData() {
        mPresenter.loadOrderList(HTTP_REFRESH_LIST, "1", 0, page_count);
    }

    @Override
    protected void loadMoreData() {
        mPresenter.loadOrderList(HTTP_LOAD_MORE_LIST, "1", page_index, page_count);
    }

    @Override
    public void whenGetOrderList(int type, List<Order> data) {
        switch (type) {
            case HTTP_INIT_LIST:
                if (data != null && data.size() > 0) {
                    handleData(data, HTTP_INIT_LIST);
                } else {
                    showNoDataDefaultView(R.mipmap.default_bg_nodata, R.string.default_no_data);
                }
                break;
            case HTTP_LOAD_MORE_LIST:
                handleData(data, HTTP_LOAD_MORE_LIST);
                break;
        }

    }

    @Override
    public void deleteSucceed(int position) {
        ToastUtil.showInfo(R.string.delete_succeed);
        adapter.remove(position);
        if (datas.size() == 0) {
            showNoDataDefaultView(R.mipmap.default_bg_nodata, R.string.default_no_data);
        }
    }

    @Override
    protected int getLaoutResouse() {
        return R.layout.fragment_order_list;
    }
}
